package com.kx.game.center.manager;

import com.kx.game.center.common.UserEnum;
import com.kx.game.center.global.GSServices;
import com.kx.game.center.bean.UserBean;
import com.kx.game.center.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserManager {

    private static Logger logger = LoggerFactory.getLogger(UserManager.class);

    //tyep+最后一位字符
    private String getTypeAndLastKey(String string, String type) {
        if (string == null || string.equals("") || string.length() == 0)
            return type;
        return type + "_" + string.substring(string.length() - 1).toUpperCase();
    }

    //玩家信息
    public UserBean initUser(String id, String wechatId, String wechatName, String iconUrl, String level, String userState,
                             String lastUpdateTime, String serverId, String region, String sex, String friendCode, String roleId) throws Exception {
        UserBean bean = new UserBean(id, wechatId, wechatName, iconUrl, level, userState, lastUpdateTime,
                serverId, region, sex, friendCode, roleId);
        saveUserInfo(bean);
        return bean;
    }

    //玩家信息
    public UserBean updateUser(String wechatName, String iconUrl, String level, String userState, String lastUpdateTime,
                               String region, String sex, String friendCode, String roleId, String serverId, UserBean bean) throws Exception {
        bean.setWechatName(wechatName);
        bean.setIconUrl(iconUrl);
        bean.setLevel(level);
        bean.setUserState(userState);
        bean.setLastUpdateTime(lastUpdateTime);
        bean.setRegion(region);
        bean.setServerId(serverId);
        bean.setSex(sex);
        bean.setFriendCode(friendCode);
        bean.setRoleId(roleId);
        saveUserInfo(bean);
        return bean;
    }

    public UserBean updateUserInfo(String id, String wechatId, String wechatName, String iconUrl, String level,
                                   String userState, String lastUpdateTime, String serverId, String region, String sex,
                                   String friendCode, String roleId, String create, UserBean userInfo) throws Exception {
        if (userInfo == null) { //初始化一下
            userInfo = GSServices.userManager.initUser(id, wechatId, wechatName, iconUrl, level, userState,
                    lastUpdateTime, serverId, region, sex, friendCode, roleId);
            if (create.equals("1"))
                GSServices.localManager.incrementServerCreateNum(serverId, 1);
        } else {
            GSServices.userManager.updateUser(wechatName, iconUrl, level, userState,
                    lastUpdateTime, region, sex, friendCode, roleId, serverId, userInfo);
        }
        return userInfo;
    }

    //玩家信息
    public UserBean getUserInfo(String wechatId) throws Exception {
        String userKey = getTypeAndLastKey(wechatId, UserEnum.UserInfo.getType());
        return GSServices.redisManager.hashGet(userKey, wechatId + "", UserBean.class);
    }

    //保存玩家信息
    public void saveUserInfo(UserBean bean) throws Exception {
        if (bean == null)
            return;
        String userKey = getTypeAndLastKey(bean.getWechatId(), UserEnum.UserInfo.getType());
        GSServices.redisManager.hashSet(userKey, bean.getWechatId() + "", bean);
    }

    //获取好友码
    public String getUserIdByFriendCode(String friendCode) throws Exception {
        String key = getTypeAndLastKey(friendCode, UserEnum.FriendCode.getType());
        return GSServices.redisManager.hashGet(key, friendCode, String.class);
    }

    //设置好友码
    public void setFriendCode(String friendCode, String wechatId) throws Exception {
        if (StringUtils.isEmpty(friendCode))
            return;
        String key = getTypeAndLastKey(friendCode, UserEnum.FriendCode.getType());
        GSServices.redisManager.hashSet(key, friendCode, wechatId);
    }

    //好友码是否存在
    public boolean existFriendCode(String friendCode) throws Exception {
        String key = getTypeAndLastKey(friendCode, UserEnum.FriendCode.getType());
        return !StringUtils.isEmpty(GSServices.redisManager.hashGet(key, friendCode, String.class));
    }

}
